package cn.itsource.web.controller;


import cn.itsource.domain.Permission;
import cn.itsource.domain.Role;
import cn.itsource.query.RoleQuery;
import cn.itsource.result.JSONResult;
import cn.itsource.serivce.IPermissionService;
import cn.itsource.serivce.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**--------------------------------------------------------
角色相关
 --------------------------------------------------------**/
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService ;


    /**--------------------------------------------------------
    角色列表
     --------------------------------------------------------**/
    @RequestMapping("/index")
    public String selectForPage(RoleQuery query,Model model){

        PageInfo<Role> page = roleService.selectForPage(query);

        model.addAttribute("pageInfo", page);

        //isFirstPage
        return "views/role/role_list";
    }


    /**--------------------------------------------------------
     角色添加
     --------------------------------------------------------**/
    @RequestMapping("/add")
    @ResponseBody
    public JSONResult add(Role role){
        try {
            roleService.insertOrUpdate(role);
            return new JSONResult();
        }catch (Exception e){
            e.printStackTrace();
            return new JSONResult(false,"啊，系统异常啦，我们正在殴打程序员...");
        }
    }
    /**--------------------------------------------------------
    跳转权限子页面
     --------------------------------------------------------**/
    @RequestMapping("/toPermission")
    public String toPermission(Long roleId,Model model){
        //查询所有权限，树状结构
        List<Permission> allPermissons = permissionService.selectAllForTree();

        //当前角色关联了的权限，扁平的list
        List<Long> rolePermissionIds = permissionService.selectIdsByRoleId(roleId);

        Map<String,Object> result = new HashMap<>() ;

        //所有的权限，树结构
        result.put("permissions", allPermissons);
        //当前角色关联了的权限，扁平的list
        result.put("myPermissionids", rolePermissionIds);
        //角色的ID
        result.put("rid", roleId);


        model.addAttribute("map", result);

        return "views/role/role_permission";
    }


    /**--------------------------------------------------------
    添加角色和权限的关系
     --------------------------------------------------------**/
    @RequestMapping("/addPermission")
    @ResponseBody
    public JSONResult addPermission(Long roleId,String permissionIds){
        try {
            roleService.insertRelationWithPermission(roleId , permissionIds);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"系统异常啦");
        }
    }

}
